<?php
/**
* Unlink all thumbnails images.
*/

#print_r($_GET);
/*
Array
(
    [page] => albumsizes-remove.php
    [album] => 3
    [size] => 2
)
*/

$album_id = $variable->get('album', 'integer', 0);
$size_id = $variable->get('size', 'integer', 0);

$success_dir=0;
$success_file=0;
$error_dir = 0;
$error_file = 0;
$errors = array();

if($size_id == THUMBNAIL_ORIGINAL_SIZE_ID)
{
	$errors[] = "Cannot remove original images.";
	
	$messenger = new messenger('error', 'Skipped deleting the originally scanned image files.');
}
else
{
	$location = realpath(FTPPATH_THUMBNAILS.'/'.$album_id.'/'.$size_id);
	if(!is_dir($location))
	{
		$errors[] = "Target is not a directory: {$location}.";
	}
	else #if(!unlink($location))
	{
		# Try to remove individual files under it
		# die('Cannot remove the entire directory: '.$location);
		
		$scan = scandir($location);
		foreach($scan as $s => $name)
		{
			if(in_array($name, array('.', '..'))) continue;
			
			$target = $location.'/'.$name;
			if(is_dir($target))
			{
				++$error_dir;
			}
			else
			{
				if(unlink($target))
				{
					++$success_file;
				}
				else
				{
					++$error_file;
				}
			}
		}
		
		if($error_dir) { $errors[] = "Untreated directories: {$error_dir}"; }
		if($error_file) { $errors[] = "Failed files removal: {$error_file}"; }
	}
	#else
	{
		#$errors[] = "Successful at a minimal shot!";
	}
	
	# do something with $errors
	# ...
	
	$messenger = new messenger('notice', "Action taken: Please verify which images are unlinked. Failed: directories({$error_dir}),  files({$error_file}), Success: directories({$success_dir}),  files({$success_file}).");
} # if

headers::back(url::last_page('albumsizes-list.php'));
?>